//
//  SYFunctionView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/11.
//  Copyright © 2019年 李文强. All rights reserved.
//

#import "SYFunctionView.h"
#import "SYFunctionTableViewCell.h"


@interface SYFunctionView () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) SYFunctionView *functionView;

@property (strong, nonatomic) UIButton *foldingBtn;//折叠bug



@property (strong, nonatomic) NSArray *dataListArr;//数据

@property (strong, nonatomic) UIView *fgxView;
@end


@implementation SYFunctionView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
        [self initSubView];
        [self initLayout];
    }
    return self;
    
}

- (void)initSubView {
    //折叠
    [self addSubview:self.foldingBtn];
    [self addSubview:self.curTableView];
    self.indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.curTableView selectRowAtIndexPath:_indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    [self addSubview:self.fgxView];
    [self.fgxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
}

- (void)initLayout {

    [self.foldingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(5);
        make.right.equalTo(self.mas_right).offset(-5);
        make.height.with.mas_equalTo(35);
    }];
    
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(44);
        make.left.right.bottom.equalTo(self);
    }];
    
}

#pragma mark - table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYFunctionTableViewCell *functionCell = [tableView dequeueReusableCellWithIdentifier:@"SYFunctionTableViewCell"];
    
    if (!functionCell) {
        functionCell = [[SYFunctionTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"SYFunctionTableViewCell"];
    }
    functionCell.selectionStyle = UITableViewCellSelectionStyleNone;
    SYFunctoinModel *model = self.dataListArr[indexPath.row];
    if (self.indexPath.row == indexPath.row) {
        model.isBg = YES;
    } else {
        model.isBg = NO;
    }
    functionCell.function = model;
    return functionCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.indexPath = indexPath;
    SYFunctoinModel *model = self.dataListArr[indexPath.row];
    if (self.block) {
        self.block(model,indexPath.row);
    }
    [self.curTableView reloadData];
}



#pragma set&&get
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] init];
        _curTableView.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _curTableView;
}

- (UIButton *)foldingBtn {
    if (!_foldingBtn) {
        _foldingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_foldingBtn setImage:[UIImage imageNamed:@"open"] forState:UIControlStateNormal];
        [_foldingBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateSelected];
        _foldingBtn.selected = NO;
        [_foldingBtn addTarget:self action:@selector(openClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _foldingBtn;
}
-(void)setViewFromType:(NSString *)type{
    self.isFrom = type;
    if ([type isEqualToString:@"fenxiang"]) {
        
    }
    [self.foldingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(5);
        make.left.equalTo(self.mas_left).offset(10);
        make.height.with.mas_equalTo(35);
    }];
}
- (void)openClick:(UIButton *)send {
    if ([self.isFrom isEqualToString:@"fenxiang"]) {
        if (self.fenXiangFromBlcok) {
            self.fenXiangFromBlcok();
        }
        return;
    }
    send.selected =!send.selected;
    if (send.selected) {
        _functionView.frame = CGRectMake(0, 20, 70, APP_HEIGHT-20);
    } else {
        _functionView.frame = CGRectMake(0, 20, 215, APP_HEIGHT-20);
    }
    for (SYFunctoinModel *function in self.dataListArr) {
        function.isFolding = send.selected;
    }
    [self.curTableView reloadData];
}
- (void)showFunctionView:(SYFunctionView *)functionView vc:(UIViewController *)vc listDataArr:(NSArray *)listArr {
    _dataListArr = listArr;
    _functionView = functionView;
//    [vc.view addSubview:functionView];
    [self.curTableView reloadData];
    
}
- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;
    [self.curTableView reloadData];
}


- (UIView *)fgxView {
    if (!_fgxView) {
        _fgxView = [UIView new];
        _fgxView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    }
    return _fgxView;
}
@end













